package com.deplume.ssm.controller.admin;

import com.deplume.ssm.common.Page;
import com.deplume.ssm.pojo.admin.AdList;
import com.deplume.ssm.pojo.admin.Advertise;
import com.deplume.ssm.pojo.admin.CompanyDetail;
import com.deplume.ssm.pojo.admin.Customer;
import com.deplume.ssm.pojo.extra.ChangeStatus;
import com.deplume.ssm.service.inface.admin.ICustomerRelativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by blake on 2016/8/2.
 */

@Controller
@RequestMapping("/Customer")
public class CustomerController {
    @Autowired
    ICustomerRelativeService customerRelativeService;

    @RequestMapping("returnCustomerInfos")
    public String returnCustomerInfos(Model model, HttpServletRequest
            request, int page)
            throws Exception
    {
        Page page1 = new Page();
        page1.setPageSize(8);
        page1.setCurrentPage(page);
        List<Customer> list = customerRelativeService.returnCustomerInfos(page1);
        model.addAttribute("list",list);
        model.addAttribute("currentPage",page1.getCurrentPage());
        return "admin/listAlluser";
    }
    @RequestMapping("returnCustomerInfo")
    public String returnCustomerInfo(Model model,HttpServletRequest
            request, int id)
            throws Exception
    {
        Customer customer = customerRelativeService.returnCustomerInfo(id);
        model.addAttribute("customer",customer);

        return "admin/listCustomerDetail";
    }
    @RequestMapping("findCustomerByName")
    public String findCustomerByName(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "currentPage",defaultValue = "1",required = false)int page, String customerName) throws Exception
    {
        Page page1 = new Page();
        page1.setPageSize(8);
        page1.setName(customerName);
        if(page == 0)
        {
            page1.setCurrentPage(1);
        }else
        {
            page1.setCurrentPage(page);
        }
        /*System.out.println(page1.getName());*/
        List<Customer> list = customerRelativeService.findCustomerByName(page1);
        /*for (Customer c: list){
            System.out.println(c.getFullName());
        }*/
        model.addAttribute("list",list);
        model.addAttribute("customerName",page1.getName());
        model.addAttribute("currentPage",page1.getCurrentPage());
        return "admin/listAlluser";
    }

    @RequestMapping("changeCustomerStatus")
    public String changeCustomerStatus(Model model,HttpServletRequest request,
                                    String page,int status,int id) throws Exception
    {
        int nowStatus = 0;
        if(status == 0)
            nowStatus = 1;
        ChangeStatus changeStatus = new ChangeStatus();
        changeStatus.setId(id);
        changeStatus.setStatus(nowStatus);

        customerRelativeService.changeCustomerStatus(changeStatus);

        return "redirect:/Customer/returnCustomerInfos.action?page="+page;

    }
    @RequestMapping("deleteCustomerById")
    public String deleteCustomerById(Model model,HttpServletRequest request,
                                     Integer id,int page) throws Exception
    {
        customerRelativeService.deleteCustomerById(id);
        return "redirect:/Customer/returnCustomerInfos.action?page="+page;
    }
    @RequestMapping("deleteCustomers")
    public String deleteCustomers(Integer[] customersId) throws Exception
    {
        for (Integer i :customersId) {
            System.out.println(i);
            customerRelativeService.deleteCustomerById(i);
        }
        return "redirect:/Customer/returnCustomerInfos.action?page=1";
    }
    @RequestMapping("updateCustomerInfo")
    public String updateCustomerInfo(Model model,HttpServletRequest request,
                                     Customer customer,int customerId) throws
            Exception
    {
        customer.setCustomerId(customerId);
        if(customer.getStrSex().equals("男"))
        {
            customer.setsSex(0);
        }else{
            customer.setsSex(1);
        }
        if(customer.getStatus().equals("正常状态")){
            customer.setsStatus(0);
        }else{
            customer.setsStatus(1);
        }
        customerRelativeService.updateCustomerInfo(customer);
        return "redirect:/Customer/returnCustomerInfo.action?id="+customerId;
    }
    @RequestMapping("returnAllAdvertisement")
    public String returnAllAdvertisement(Model model, HttpServletRequest
            request, int page) throws Exception
    {
        Page page1 = new Page();
        page1.setPageSize(8);
        page1.setCurrentPage(page);

        List<Advertise> list = customerRelativeService.returnAllAdvertisement
                (page1);

        model.addAttribute("currentPage",page1.getCurrentPage());
        model.addAttribute("list",list);
        return "admin/listAllAdvertisement";
    }
    @RequestMapping("findAdvertisementByName")
    public String findAdvertisementByName(Model model, HttpServletRequest
            request, HttpServletResponse response, @RequestParam(value =
            "currentPage",defaultValue = "1",required = false)int page,
                                          String advertisementName) throws Exception
    {
        Page page1 = new Page();
        page1.setPageSize(8);
        page1.setName(advertisementName);
        if(page == 0)
        {
            page1.setCurrentPage(1);
        }else
        {
            page1.setCurrentPage(page);
        }
        List<Advertise> list = customerRelativeService
                .findAdvertisementByName(page1);
        model.addAttribute("list",list);
        model.addAttribute("advertisementName",page1.getName());
        model.addAttribute("currentPage",page1.getCurrentPage());
        return "admin/listAllAdvertisement";

    }
    @RequestMapping("changeAdvertisementStatus")
    public String changeAdvertisementStatus(Model model, HttpServletRequest
    request, HttpServletResponse response, String page,int status,int id)
            throws Exception
    {
        int nowStatus = 0;
        if(status == 0)
            nowStatus = 1;
        ChangeStatus changeStatus = new ChangeStatus();
        changeStatus.setId(id);
        changeStatus.setStatus(nowStatus);
        customerRelativeService.changeAdvertisementStatus(changeStatus);
        return "redirect:/Customer/returnAllAdvertisement.action?page="+page;
    }
    @RequestMapping("addAdvertisement")
    public String addAdvertisement(Model model, HttpServletRequest request)
            throws Exception
    {
        return "admin/addAdvertisement";
    }

    @RequestMapping("insertAdvertisement")
    public String insertAdvertisement(Model model, HttpServletRequest
            request, HttpServletResponse response, Advertise advertise)
            throws Exception
    {
        if(advertise.getStrid().equals("0")){
            advertise.setStatus(0);
        }else {
            advertise.setStatus(1);
        }
        customerRelativeService.insertAdvertisement(advertise);
        return "redirect:/Customer/returnAllAdvertisement.action?page=1";
    }
    @RequestMapping("updateAdvertisementById")
    public String updateAdvertisementById(Model model, HttpServletRequest
            request, AdList adList, String itemId)
            throws Exception
    {
        String[] idlist = adList.getStrid();
        String[] namelist = adList.getName();
        String[] urllist = adList.getUrl();
        String[] linklist = adList.getLink();
        int m=0;
        for(int n = 0; n < idlist.length; n++){
            if(idlist[n].equals(itemId)){
                m = n;
                break;
            }
        }
        Advertise advertise = new Advertise();
        int temp_id = Integer.valueOf(idlist[m]);
        advertise.setId(temp_id);
        advertise.setName(namelist[m]);
        advertise.setLink(linklist[m]);
        advertise.setUrl(urllist[m]);

        customerRelativeService.updateAdvertisementById(advertise);
        return "redirect:/Customer/returnAllAdvertisement.action?page=1";
    }
    @RequestMapping("deleteAdvertisementById")
    public String deleteAdvertisementById(Model model,HttpServletRequest request,
                                          Integer id,int page) throws Exception
    {
        customerRelativeService.deleteAdvertisementById(id);
        return "redirect:/Customer/returnAllAdvertisement.action?page="+page;
    }
    @RequestMapping("deleteAdvertisements")
    public String deleteAdvertisements(Integer[] advertiseId) throws Exception
    {
        for (Integer i:advertiseId){
            customerRelativeService.deleteAdvertisementById(i);
        }
        return "redirect:/Customer/returnAllAdvertisement.action?page=1";
    }

    /*@RequestMapping("editCompanyDetails")
    public String editCompanyDetails(Model model,HttpServletRequest request,
                                     @RequestParam(required = false)
                                             MultipartFile companyImg,
                                     @RequestParam(required = false)
                                             MultipartFile companyLicenceUrl,
                                     CompanyDetail
                                             companyDetail)throws Exception{
        System.out.println("companyDetailId:"+companyDetail.getCompanydetailId());
        System.out.println("companyId:"+companyDetail.getCompanyId());
        String[] args = companyDetail.getSecIndustryName().split("/");
        List<String> list = new ArrayList<>();
        for (String name:args){
            System.out.println("name"+name);
            list.add(name);
        }
        System.out.println("mainindustryName================:"+companyDetail.getMainIndustryName());
        int mainIndustryId = companyRelativeService.selectMainIndustryId("%"+companyDetail.getMainIndustryName()+"%");
        System.out.println("id================:"+mainIndustryId);
        int companySizeId = companyRelativeService.selectCompanySizeId("%"+companyDetail.getCompanySizeName()+"%");
        System.out.println("id================:"+companySizeId);
        int companyPropertyId = companyRelativeService.selectCompanyPropertyId("%"+companyDetail.getCompamyPropertyName()+"%");
        System.out.println("id================:"+companyPropertyId);
        companyDetail.setMainindustryId(mainIndustryId);
        companyDetail.setCompanysizeId(companySizeId);
        companyDetail.setCompanypropertyId(companyPropertyId);

        companyRelativeService.deleteSecIndustry(companyDetail.getCompanydetailId());
        companyRelativeService.insertSecIndustry(companyDetail.getCompanydetailId(),list);

        String originalFileName = companyImg.getOriginalFilename();
        if((companyImg != null) && (originalFileName != null) &&
                (originalFileName
                        .length() > 0)){
            String image_path = "F:\\image";
            String newFileName = UUID.randomUUID() + originalFileName
                    .substring(originalFileName.lastIndexOf("."));
            File newFile = new File(image_path + newFileName);
            companyImg.transferTo(newFile);
            companyDetail.setLogo(newFileName);
        }

        originalFileName = companyLicenceUrl.getOriginalFilename();
        if(companyLicenceUrl != null && originalFileName != null &&
                originalFileName.length() > 0){
            String url_path = "F:\\image";
            String newFileName = UUID.randomUUID() + originalFileName
                    .substring(originalFileName.lastIndexOf("."));
            File newFile = new File(url_path + newFileName);
            companyLicenceUrl.transferTo(newFile);
            companyDetail.setLicenceUrl(newFileName);
        }
        System.out.println(companyDetail.getCompanyId());
        companyRelativeService.updateCompanyDetailInfo(companyDetail);

        return "redirect:/admin/selectCompanyDetail.action?companyId="+companyDetail.getCompanyId();
    }*/

}
